package com.ktjiaoyu.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
public class DaoConfig {
    @Bean
    //配置SqlSessionFactoryBean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean=
                new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setTypeAliasesPackage("com.ktjiaoyu.entity");
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:com/ktjiaoyu/mapper/**/*.xml"));
        return sqlSessionFactoryBean;
    }

    @Bean
    //配置Mapper扫描器，扫描指定包中所有接口，自动注册对应的MapperFactoryBean对象
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.ktjiaoyu.mapper");

        return mapperScannerConfigurer;
    }
}
